#include "StudentTest.h"
#include <string>
#include <fstream>
#include <sstream>

std::string StudentTest::List() {
	std::string list;

	for (std::vector<Question>::iterator it = v.begin(); it != v.end(); ++it) {
		list += it->GetQuestion();
		list += " ";
		list += it->GetRightAnswer();
		list += " ";
		list += std::to_string(it->GetPointRightAnswer());
		list += " ";
		list += it->GetStudentAnswer();
		list += " ";
		list += std::to_string(it->GetPointStudentAnswer());
		list += "\n";
	}

	return list;
}

void StudentTest::WriteToFile() {
	std::ofstream ofs;
	ofs.open("testdata.txt");
	ofs << List();
	ofs.close();
}

void StudentTest::ReadFromFile(std::string file) {
	std::string s;
	std::ifstream ifs(file);
	if (ifs.is_open()) {
		while (std::getline(ifs, s)) {
			Question que;
			std::string q, a, sa;
			double m, sp;
			std::istringstream iss(s);
			iss >> q >> a >> m >> sa >> sp;
			que.SetQuestion(q);
			que.SetRightAnswer(a);
			que.SetPointRightAnswer(m);
			que.SetStudentAnswer(sa);
			que.SetPointStudentAnswer(sp);
			v.push_back(que);
		}
		ifs.close();
	}
}

int StudentTest::CheckWholeTest() {
	double count = 0, score = 0, percent;
	for (std::vector<Question>::iterator it = v.begin(); it != v.end(); ++it) {
		if (it->Check()) {
			score++;
		}
		count++;
	}

	return (score / count) * 100;
}